//
//  AppDelegate.swift
//  微博(Swift)
//
//  Created by V on 15/12/26.
//  Copyright © 2015年 V. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // 注册通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootViewContrller:", name: FXSwitchRootViewControllerNotification, object: nil)
        
        // 1. 创建window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        // 2. 设置根控制器
        if hasNewVersion(){
            window?.rootViewController = FXNewFeatureViewController()
        }else{
        window?.rootViewController = FXUserAccountViewModel.sharedViewModel.userLogin ? defaultRootViewController() : FXTabBarController()
        }
        // 3. 成为主窗口，并且可见
        window?.makeKeyAndVisible()
        
        return true
    }
    
    // 根据版本号 判断跳转的控制器
    private func defaultRootViewController() ->UIViewController{
        if hasNewVersion(){
            return FXNewFeatureViewController()
        }else{
            return FXWelcomeViewController()
        }
    }
    
    @objc private func switchRootViewContrller(notify: NSNotification){
        if notify.object == nil{
            window?.rootViewController = FXTabBarController()
        }else{
            window?.rootViewController = FXWelcomeViewController()
        }
    }
    
    // 移除通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    
    // 判断是否有新版本
    private func hasNewVersion() ->Bool{
        // 1.从沙盒里面取存的版本
        let sanboxVersion = NSUserDefaults.standardUserDefaults().stringForKey("version")
        
        // 3.取到当前app的版本号
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        // 4.保存当前版本号
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: "version")
        
        // 2.判断 沙盒中是否取出来了 版本号
        if sanboxVersion == nil {
            return true
        }
        // 5.当前 app 的版本号大于沙盒里面的版本号，就代表有新版本
        if currentVersion.compare(sanboxVersion!) == .OrderedDescending{
            return true
        }
        return false
    }
    
    

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

